<?php

namespace plugin\admin\app\controller;

use support\Request;
use support\Response;
use plugin\admin\app\model\ThSystemCrontab;
use support\exception\BusinessException;

/**
 * 定时任务 
 */
class ThSystemCrontabController extends Crud
{
    
    /**
     * @var ThSystemCrontab
     */
    protected $model = null;


    protected $allowField = ['title', 'type', 'rule', 'target', 'parameter', 'remark', 'sort', 'status', 'singleton'];

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new ThSystemCrontab;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('th-system-crontab/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $data = $this->insertInput($request);
            foreach ($data as $k => $v) {
                if (in_array($k, $this->allowField)) {
                    $args[$k] = $v;
                }
            }
            $param = [
                'method' => 'crontabCreate',
                'args'   => $args
            ];
            $result  = \yzh52521\Task\Client::instance()->request($param);
            if ($result->data->code) {
                return $this->json(0, '添加成功');
            }
            throw new BusinessException('添加失败');
        }
        return view('th-system-crontab/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            [$id, $data] = $this->updateInput($request);
            $model = $this->model->find($id);
            if (!$model) {
                throw new BusinessException('记录不存在', 2);
            }
            $args['id'] = $id;
            foreach ($data as $key => $val) {
                if (in_array($key, $this->allowField)) {
                    $args[$key] = $val;
                }
            }
            $param = [
                'method' => 'crontabUpdate',
                'args'   => $args
            ];
            $result  = \yzh52521\Task\Client::instance()->request($param);
            if ($result->data->code) {
                return $this->json(0, '修改成功');
            }
            throw new BusinessException('修改失败');
        }
        return view('th-system-crontab/update');
    }

    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        $param = [
            'method' => 'crontabDelete',
            'args'   => [
                'id'     => implode(',', $ids)
            ]
        ];
        $result  = \yzh52521\Task\Client::instance()->request($param);
        if ($result->data->code) {
            return $this->json(0, '删除成功');
        }
        throw new BusinessException('删除失败');
    }

    public function reboot(Request $request): Response
    {
        $ids = $this->handleInput($request);
        $param = [
            'method' => 'crontabReload',
            'args'   => [
                'id'     => implode(',', $ids)
            ]
        ];
        $result  = \yzh52521\Task\Client::instance()->request($param);
        if ($result->data->code) {
            return $this->json(0, '重启成功');
        }
        throw new BusinessException('重启失败');
    }

}
